#include "countrymap.h"
#include "ui_countrymap.h"
#include <QPushButton>
#include <QString>
#include <QPainter>
#include <QMouseEvent>
#include <vector>
#include "city.h"
#include "enterbutton.h"
#include "citymap.h"
#include <QTimer>

const int INF = 0x3f3f3f3f;
const int NN = 10;


// 城市窗口类
CountryMap::CountryMap(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CountryMap)
{

    ui->setupUi(this);

    this->getx["jinan"]=200;
    this->gety["jinan"]=200;
    this->getwidth["jinan"]=51;
    this->getheight["jinan"]=91;

    this->getx["beijing"]=ui->beijing->pos().rx();
    this->gety["beijing"]=ui->beijing->pos().ry();
    this->getwidth["beijing"]=ui->beijing->width();
    this->getheight["beijing"]=ui->beijing->height();

    this->getx["shanghai"]=ui->shanghai->pos().rx();
    this->gety["shanghai"]=ui->shanghai->pos().ry();
    this->getwidth["shanghai"]=ui->shanghai->width();
    this->getheight["shanghai"]=ui->shanghai->height();

    this->getx["xiamen"]=ui->xiamen->pos().rx();
    this->gety["xiamen"]=ui->xiamen->pos().ry();
    this->getwidth["xiamen"]=ui->xiamen->width();
    this->getheight["xiamen"]=ui->xiamen->height();

    this->getx["liaoning"]=ui->liaoning->pos().rx();
    this->gety["liaoning"]=ui->liaoning->pos().ry();
    this->getwidth["liaoning"]=ui->liaoning->width();
    this->getheight["liaoning"]=ui->liaoning->height();

    this->getx["xinjiang"]=ui->xinjiang->pos().rx();
    this->gety["xinjiang"]=ui->xinjiang->pos().ry();
    this->getwidth["xinjiang"]=ui->xinjiang->width();
    this->getheight["xinjiang"]=ui->xinjiang->height();

    this->getx["chengdu"]=ui->chengdu->pos().rx();
    this->gety["chengdu"]=ui->chengdu->pos().ry();
    this->getwidth["chengdu"]=ui->chengdu->width();
    this->getheight["chengdu"]=ui->chengdu->height();







    // 济南特殊按钮

    jinan->setText("济南");
    jinan->setObjectName(QString::fromUtf8("jinan"));
    jinan->setGeometry(QRect(140, 220, 111, 71));
    QIcon iconjinan;
    iconjinan.addFile(QString::fromUtf8(":/res/jinan.png"), QSize(), QIcon::Normal, QIcon::Off);
    jinan->setIcon(iconjinan);
    jinan->setIconSize(QSize(50, 50));
    jinan->setParent(ui->map);




    this->setWindowTitle("多功能导航系统V1.0");
    this->setWindowIcon(QIcon("://res/nav.png"));
    this->setFixedSize(1120,693);
    //返回按钮的实现
    connect(ui->BacktoStartBtn,&QPushButton::clicked,this,&CountryMap::BacktoStart);
//    connect(cmp,&citymap::BacktoCountry,[=](){

//        this->show();
//        cmp->hide();
//    });
    // 地图窗口样式
    ui->map->setStyleSheet(QString::fromUtf8("#map{border:3px solid black}"));


    //添加起点
    /****注意！这里排放顺序要和在邻接矩阵的顺序一致！*****/
    ui->startBox->addItem("济南");
    ui->startBox->addItem("北京");
    ui->startBox->addItem("上海");
    ui->startBox->addItem("厦门");
    ui->startBox->addItem("辽宁");
    ui->startBox->addItem("新疆");
    ui->startBox->addItem("成都");
    //添加终点
    ui->endBox->addItem("济南");
    ui->endBox->addItem("北京");
    ui->endBox->addItem("上海");
    ui->endBox->addItem("厦门");
    ui->endBox->addItem("辽宁");
    ui->endBox->addItem("新疆");
    ui->endBox->addItem("成都");

    /*
        注意：是否要写一个判断，如果起点终点相同则弹出提示。
    */

    //地图定位信息的实现（单击事件）
    /*
     留一个可优化的点，如何将这个模块的抽象层次提高
    */
    connect(jinan,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");//清空原有信息
        QString cityname = "济南";
        int cityid = c.getId["jinan"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }

    });
    connect(ui->beijing,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "北京";
        int cityid = c.getId["beijing"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }

    });
    connect(ui->shanghai,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "上海";
        int cityid = c.getId["shanghai"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }

    });
    connect(ui->xiamen,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "厦门";
        int cityid = c.getId["xiamen"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }
    });
    connect(ui->liaoning,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "辽宁";
        int cityid = c.getId["liaoning"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }
    });
    connect(ui->xinjiang,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "新疆";
        int cityid = c.getId["xinjiang"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }
    });
    connect(ui->chengdu,&QPushButton::clicked,[=](){
        ui->nodeinfo->setText("");
        QString cityname = "成都";
        int cityid = c.getId["chengdu"];
        ui->nodeinfo->insertPlainText("城市名称： "+cityname+"\n"
                              +"城市坐标： ("+QString::number(c.city[cityid].x,10)
                              +","+QString::number(c.city[cityid].y,10)+")\n"
                              +"相邻城市： \n");
        int t=0;
        for(int i=0;i<c.citynum;i++){
            if(c.dis[cityid][i]!=INF){
                ui->nodeinfo->insertPlainText(cityname+"距离"+
                                      QString::fromStdString(c.city[i].name)
                                      +QString::number(c.dis[cityid][i],10)+"公里\n");
                t++;
            }
        }
        if(t==0){
            ui->nodeinfo->insertPlainText("该城市没有相邻城市！");
        }
    });

    /*****************重要功能——导航功能********************/
    connect(ui->beginBtn,&QPushButton::clicked,[=](){

        ui->navinfo->setText("");
        //起点终点编号id
        int startid = ui->startBox->currentIndex();
        int endid = ui->endBox->currentIndex();
        ui->navinfo->insertPlainText("从"+QString::fromStdString(c.city[startid].name)+"出发，到"+QString::fromStdString(c.city[endid].name)+"\n\n");
        int flag=0;
        //出行方式的获取
        if(ui->train->isChecked()){
            ui->navinfo->insertPlainText("选择高铁\n\n");
            flag=1;
        }
        else if(ui->car->isChecked()){
            ui->navinfo->insertPlainText("选择驾车\n\n");
            flag=1;
        }
        else if(ui->walk->isChecked()){
            ui->navinfo->insertPlainText("步行距离太远啦！换其他方式吧！\n");
        }
        else if(ui->bike->isChecked()){
            ui->navinfo->insertPlainText("骑行距离太远啦！换其他方式吧！\n");
        }

        //出行策略的获取
        if(flag){
            if(ui->timefirst->isChecked()){
                ui->navinfo->insertPlainText("时间优先\n\n");
            }
            if(ui->disfirst->isChecked()){
                ui->navinfo->insertPlainText("距离优先\n\n");
            }

            /***********************最短路算法***********************/
            //Dijkstra+路径记录//Floyd先计算全局最短路径
//            passcitynum = 0;
            int totm[NN][NN];
            for(int i=0;i<c.citynum;i++){
                for(int j=0;j<c.citynum;j++){
                    totm[i][j]=c.dis[i][j];
                }
            }

            for(int k=0;k<c.citynum;k++){
                for(int i=0;i<c.citynum;i++){
                    for(int j=0;j<c.citynum;j++){
                        if(totm[i][j]>c.dis[i][k]+c.dis[k][j])
                            totm[i][j]=c.dis[i][k]+c.dis[k][j];
                    }
                }
            }

            //时间优先
            int u=startid;
            if(ui->timefirst->isChecked()){
                //选择汽车
                if(ui->car->isChecked()){
                    double mint[NN];
                    int pre[NN];
                    int vis[NN];
                    memset(mint,INF,sizeof mint);
                    memset(pre,-1,sizeof pre);
                    memset(vis,0,sizeof vis);
                    for(int i=0;i<c.citynum;i++){
                        mint[i]=c.spc[u][i];
                    }
                    for(int i=0;i<c.citynum;i++){
                        if(mint[i]<INF/2)
                            pre[i]=startid;//有通路的时候是前驱均为起点
                        else {
                            pre[i]=-1;//没通路的时候前驱为-1
                        }
                    }//记录前一个点
                    vis[u]=1;
                    int ru=u;
                    for(int i=1;i<c.citynum;i++){
                        double minn=INF/2;
                        for(int j=0;j<c.citynum;j++){
                            if(mint[j]<minn&&!vis[j]){
                                minn=mint[j];
                                ru=j;
                            }
                        }
                        vis[ru]=1;
                        for(int j=0;j<c.citynum;j++){
                            if(c.spc[ru][j]<INF/2&&mint[j]>minn+c.spc[ru][j]){
                                mint[j]=minn+c.spc[ru][j];
                                pre[j]=ru;
                            }
                        }
                    }
                    if(pre[endid]==-1){
                        ui->navinfo->insertPlainText("暂无通路\n");
                    }
                    else{
                        int ii=endid;
                        int re[NN];
                        int k=0;
                        re[k++]=endid;
                        while(pre[ii]!=startid){
                            re[k++]=pre[ii];
                            ii=pre[ii];
                        }
                        re[k]=startid;
                        ui->navinfo->insertPlainText("全程"+QString::number(totm[startid][endid],10)+"公里\n");
                        ui->navinfo->insertPlainText("大约需要"+QString::number(mint[endid])+"小时\n");
                        ui->navinfo->insertPlainText("沿途经过\n");
                        int i=0;
                        for(int j=k;j>=0;j--){
                            if(j!=0){
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"->");

                            }
                            else {
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"\n");
                            }
                        }
                        this->cx[i]=-1;
                        this->cy[i]=-1;
                    }
                }

                //选择高铁
                else if(ui->train->isChecked()){
                    double mind[NN];
                    int pree[NN];
                    int viss[NN];
                    memset(mind,INF,sizeof mind);
                    memset(pree,-1,sizeof pree);
                    memset(viss,0,sizeof viss);
                    for(int i=0;i<c.citynum;i++){
                        mind[i]=c.spt[u][i];
                    }
                    for(int i=0;i<c.citynum;i++){
                        if(mind[i]<INF/2)
                            pree[i]=startid;//有通路的时候是前驱均为起点
                        else {
                            pree[i]=-1;//没通路的时候前驱为-1
                        }
                    }//记录前一个点
                    viss[u]=1;
                    int ru=u;
                    for(int i=1;i<c.citynum;i++){
                        double minn=INF/2;
                        for(int j=0;j<c.citynum;j++){
                            if(mind[j]<minn&&!viss[j]){
                                minn=mind[j];
                                ru=j;
                            }
                        }
                        viss[ru]=1;
                        for(int j=0;j<c.citynum;j++){
                            if(c.spt[ru][j]<INF/2&&mind[j]>minn+c.spt[ru][j]){
                                mind[j]=minn+c.spt[ru][j];
                                pree[j]=ru;
                            }
                        }
                    }
                    if(pree[endid]==-1){
                        ui->navinfo->insertPlainText("暂无通路\n");
                    }
                    else{
                        int ii=endid;
                        int re[NN];
                        int k=0;
                        re[k++]=endid;
                        while(pree[ii]!=startid){
                            re[k++]=pree[ii];
                            ii=pree[ii];
                        }
                        re[k]=startid;
                        ui->navinfo->insertPlainText("全程"+QString::number(totm[startid][endid],10)+"公里\n");
                        ui->navinfo->insertPlainText("大约需要"+QString::number(mind[endid])+"小时\n");
                        ui->navinfo->insertPlainText("沿途经过\n");
//                        passcitynum = k;
                        int i=0;
                        for(int j=k;j>=0;j--){
                            if(j!=0){
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"->");
                            }
                            else {
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"\n");
                            }
                        }
                        this->cx[i]=-1;
                        this->cy[i]=-1;
                    }
                }
            }

            //距离最短
            else if(ui->disfirst->isChecked()){
                //选择汽车
                if(ui->car->isChecked()||ui->train->isChecked()){
                    int mmin[NN];
                    int ppre[NN];
                    int vvis[NN];
                    memset(mmin,INF,sizeof mmin);
                    memset(ppre,-1,sizeof ppre);
                    memset(vvis,0,sizeof vvis);
                    for(int i=0;i<c.citynum;i++){
                        mmin[i]=c.dis[u][i];
                    }
                    for(int i=0;i<c.citynum;i++){
                        if(mmin[i]<INF/2)
                            ppre[i]=startid;//有通路的时候是前驱均为起点
                        else {
                            ppre[i]=-1;//没通路的时候前驱为-1
                        }
                    }//记录前一个点
                    vvis[u]=1;
                    int ru=u;
                    for(int i=1;i<c.citynum;i++){
                        int minn=INF/2;
                        for(int j=0;j<c.citynum;j++){
                            if(mmin[j]<minn&&!vvis[j]){
                                minn=mmin[j];
                                ru=j;
                            }
                        }
                        vvis[ru]=1;
                        for(int j=0;j<c.citynum;j++){
                            if(c.dis[ru][j]<INF/2&&mmin[j]>minn+c.dis[ru][j]){
                                mmin[j]=minn+c.dis[ru][j];
                                ppre[j]=ru;
                            }
                        }
                    }
                    if(ppre[endid]==-1){
                        ui->navinfo->insertPlainText("暂无通路\n");
                    }
                    else{
                        int ii=endid;
                        int re[NN];
                        int k=0;
                        re[k++]=endid;
                        while(ppre[ii]!=startid){
                            re[k++]=ppre[ii];
                            ii=ppre[ii];
                        }
                        re[k]=startid;
                        ui->navinfo->insertPlainText("全程"+QString::number(totm[startid][endid],10)+"公里\n");
                        ui->navinfo->insertPlainText("大约需要"+QString::number(mmin[endid])+"小时\n");
                        ui->navinfo->insertPlainText("沿途经过\n");
//                        passcitynum = k;
                        int i=0;
                        for(int j=k;j>=0;j--){
                            if(j!=0){
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"->");
                            }
                            else {
                                this->cx[i]=this->getx[c.city[re[j]].ename]+this->getwidth[c.city[re[j]].ename];
                                this->cy[i]=this->gety[c.city[re[j]].ename]+this->getheight[c.city[re[j]].ename];
                                i++;
                                ui->navinfo->insertPlainText(QString::fromStdString(c.city[re[j]].name)+"\n");
                            }
                        }
                        this->cx[i]=-1;
                        this->cy[i]=-1;
                    }
                }
                //选择高铁
                //理想情况下，高铁和汽车同行距离相同
            }
        }
        cout<<this->cx[1]<<" "<<this->cy[1]<<endl;
        this->repaint();



        /*******************************************************/


    });


}

CountryMap::~CountryMap()
{
    delete ui;
}

void CountryMap::paintEvent(QPaintEvent *ev)
{
    QPainter painter(this);
    QPixmap pix,pix2;
    // 画背景图
    pix.load(":/res/mapbg.jpeg");
    painter.drawPixmap(ui->map->geometry(),pix);
    //画坐标
    pix2.load(":/res/cursor.png");
    painter.drawPixmap(px-15,py-50,40,50,pix2);
    //画初始道路
    QPen pen1,pen2;
//    pen1.setWidth(1);
//    pen1.setColor(QColor(Qt::black));
//    pen2.setWidth(3);
    pen2.setColor(QColor(Qt::red));
    painter.setPen(pen2);
    painter.setPen(pen1);
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())));
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())));
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())));
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())));
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())));
    painter.drawLine(QPoint(jinan->pos().x()+(jinan->width()),jinan->pos().y()+(jinan->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));

    painter.drawLine(QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())),
                     QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())));
    painter.drawLine(QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())),
                     QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())));
    painter.drawLine(QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())),
                     QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())));
    painter.drawLine(QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())),
                     QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())));
    painter.drawLine(QPoint(ui->beijing->pos().x()+(ui->beijing->width()),ui->beijing->pos().y()+(ui->beijing->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));

    painter.drawLine(QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())),
                     QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())));
    painter.drawLine(QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())),
                     QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())));
    painter.drawLine(QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())),
                     QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())));
    painter.drawLine(QPoint(ui->shanghai->pos().x()+(ui->shanghai->width()),ui->shanghai->pos().y()+(ui->shanghai->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));

    painter.drawLine(QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())),
                     QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())));
    painter.drawLine(QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())),
                     QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())));
    painter.drawLine(QPoint(ui->xiamen->pos().x()+(ui->xiamen->width()),ui->xiamen->pos().y()+(ui->xiamen->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));

    painter.drawLine(QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())),
                     QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())));
    painter.drawLine(QPoint(ui->liaoning->pos().x()+(ui->liaoning->width()),ui->liaoning->pos().y()+(ui->liaoning->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));

    painter.drawLine(QPoint(ui->xinjiang->pos().x()+(ui->xinjiang->width()),ui->xinjiang->pos().y()+(ui->xinjiang->height())),
                     QPoint(ui->chengdu->pos().x()+(ui->chengdu->width()),ui->chengdu->pos().y()+(ui->chengdu->height())));
    if(cx[0]!=0 && cy[0]!=0){
        pen2.setWidth(3);
        pen2.setColor(QColor(Qt::red));
        painter.setPen(pen2);

        for(int i=0;i<7;i++){
            if(cx[i+1]==-1 && cy[i+1]==-1) break;
            painter.drawLine(QPoint(cx[i],cy[i]),QPoint(cx[i+1],cy[i+1]));
        }
    }



}

void CountryMap::mousePressEvent(QMouseEvent *ev)
{
    int x = ev->x();
    int y = ev->y();
    if(x>=70 && x<=(70+571) && y>=40 && y<=(40+561)){
        Qt::MouseButton mbtn = ev->button();
        if(mbtn==Qt::LeftButton){
            this->px=x;
            this->py=y;
            this->repaint();
            cout<<this->px<<" "<<this->py<<endl;
        }
    }
}

//void CountryMap::mouseDoubleClickEvent(QMouseEvent *ev)
//{
//    int x = ev->x();
//    int y = ev->y();
//    int jx = jinan->pos().rx()+jinan->width()/2;
//    int jy = jinan->pos().ry()+jinan->height()/2;
//    if(x>=jx && x<=jx+jinan->width() && y>=jy && y<jy+jinan->height()){
//        this->hide();
//    }
//}
